<?php
require_once ('../../../src/db/connectdatabase.php');
require_once ("../../dao/AlbumDAO.php");

function randomCharacter() {
    $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 8; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}

$result = "null";
if(isset($_POST['act'])) {
	$action = $_POST['act'];
	if($action == 'upa') {
		if(isset($_POST['albumname']) && isset($_POST['desralbum']) && isset($_POST['linkre']) && isset($_POST['desre'])) {
			$idalbum = time();
			$namealbum = $_POST['albumname'];
			$descalbum = $_POST['desralbum'];
			
			if(insertAlbum($idalbum, $namealbum, $descalbum)) {
				$result = $idalbum;
				$idphoto = time().randomCharacter();
				$namephoto = $_POST['desre'];
				$description = $_POST['desre'];
				$linkimage = $_POST['linkre'];
				if(insertAlbumDetail($idphoto, $idalbum, $namephoto, $description, $linkimage)) {
					
				}
			}
		}
	} else if($action == 'upd') {
		if(isset($_POST['id']) && isset($_POST['linkre']) && isset($_POST['desre'])) {
			$idalbum = $_POST['id'];
			$result = $idalbum;
			$idphoto = time().randomCharacter();
			$namephoto = $_POST['desre'];
			$description = $_POST['desre'];
			$linkimage = $_POST['linkre'];
			if(insertAlbumDetail($idphoto, $idalbum, $namephoto, $description, $linkimage)) {
				
			}
		}
	}
}

echo($result);
?>